<template>
  <div>
    <div id="app">
      <div class="container">
        <div class="close"><span class="iconfont iconicon-test"></span></div>
        <div class="logo"><span class="iconfont iconnew"></span></div>
        <div class="inputs">
          <van-form ref="form">
            <van-field
              v-model="user.username"
              name="用户名"
              placeholder="用户名"
              :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
              v-model="user.password"
              type="password"
              name="密码"
              placeholder="密码"
              :rules="[{ required: true, message: '请填写密码' }]"
            />

            <p class="tips">
              没有账号？
              <a href="#/register" class="">去注册</a>
            </p>
            <div style="margin: 16px">
              <van-button round block type="danger" @click="btnlogin"
                >登录</van-button
              >
            </div>
          </van-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "../api/user";
export default {
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    btnlogin() {
      this.$refs.form
        .validate()
        .then(() => {
          console.log("检验通过");
          login(this.user).then((res) => {
            // console.log(res);
            if (res.data.message === "登录成功") {
              this.$toast.success(res.data.message);
              //保存token和用户ID
              localStorage.setItem("ly-14-token", res.data.data.token);
              localStorage.setItem("ly-14-userId", res.data.data.user.id);
              this.$router.push({
                path: "/index",
              });
            } else {
              this.$toast.fail(res.data.message);
            }
          });
        })
        .catch((err) => {
          console.log("检验不通过");
        });
      // console.log(this.user);
    },
  },
};
</script>

<style lang="less" scoped>
.van-cell::after {
  border-bottom: 1px solid #5a5756;
}
.container {
  padding: 20px;
}
.close {
  span {
    font-size: (27 / 360) * 100vw;
  }
}
.logo {
  display: flex;
  justify-content: center;

  span {
    display: block;
    font-size: (126 / 360) * 100vw;
    color: #d81e06;
  }
}

.tips {
  text-align: right;
  margin-bottom: 20px;
  margin-top: 20px;
  a {
    color: #3385ff;
  }
}
</style>